package com.py.warehouse.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.math.BigDecimal;
import java.util.List;

import com.py.warehouse.domain.WarehouseWithTotal;
import com.py.warehouse.domain.WarehouseWithTotalMoney;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.py.warehouse.mapper.BsWarehouseMapper;
import com.py.warehouse.domain.BsWarehouse;
import com.py.warehouse.service.IBsWarehouseService;

import javax.annotation.Resource;

/**
 * 仓库列表Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-25
 */
@Service
public class BsWarehouseServiceImpl implements IBsWarehouseService
{
    @Resource
    private BsWarehouseMapper bsWarehouseMapper;


    /**
     * 库存金额占比
     */
    @Override
    public WarehouseWithTotalMoney selectBsWarehouseMoney() {
        List<BsWarehouse> bsWarehouses = bsWarehouseMapper.selectBsWarehouseMoney();
        BigDecimal moneyVessel = BigDecimal.ZERO; // 初始化总和为0
        for (BsWarehouse bsWarehouse : bsWarehouses) {
            BigDecimal vessel = bsWarehouse.getInventoryValue();
            moneyVessel = moneyVessel.add(vessel); // 累加每个仓库的vessel值
            bsWarehouse.setMoneyVessel(moneyVessel); // 设置每个仓库的总计
            System.out.println("金额:"+vessel);
        }
        System.out.println("总计：" + moneyVessel);
        return new WarehouseWithTotalMoney(bsWarehouses, moneyVessel);
    }

    /**
     * 库存数量占比
     */
    @Override
    public WarehouseWithTotal selectBsWarehouseQuantity() {
        List<BsWarehouse> bsWarehouses = bsWarehouseMapper.selectBsWarehouseQuantity();
        Integer totalVessel = 0; // 初始化总和为0
        for (BsWarehouse bsWarehouse : bsWarehouses) {
            int vessel = bsWarehouse.getCurrentInventory();
            totalVessel += vessel; // 累加每个仓库的vessel值

            // 注意：这里将 totalVessel 设置回 bsWarehouse 可能会导致数据不准确
            // 因为每次循环都会覆盖之前的 totalVessel 值
            // 如果你需要保留每个仓库的总计，考虑使用其他方式存储
            bsWarehouse.setTotalVessel(totalVessel); // 设置每个仓库的总计
        }
        System.out.println("总计：" + totalVessel);
        return new WarehouseWithTotal(bsWarehouses, totalVessel);
    }

    /**
     * 查询仓库列表
     * 
     * @param warehouseId 仓库列表主键
     * @return 仓库列表
     */
    @Override
    public BsWarehouse selectBsWarehouseByWarehouseId(Long warehouseId)
    {
        return bsWarehouseMapper.selectBsWarehouseByWarehouseId(warehouseId);
    }

    /**
     * 查询仓库列表列表
     * 
     * @param bsWarehouse 仓库列表
     * @return 仓库列表
     */
    @Override
    public List<BsWarehouse> selectBsWarehouseList(BsWarehouse bsWarehouse)
    {
        return bsWarehouseMapper.selectBsWarehouseList(bsWarehouse);
    }

    /**
     * 新增仓库列表
     * 
     * @param bsWarehouse 仓库列表
     * @return 结果
     */
    @Override
    public int insertBsWarehouse(BsWarehouse bsWarehouse)
    {
        bsWarehouse.setCrateBy(SecurityUtils.getUsername());
        bsWarehouse.setCrateTime(new Date());
        return bsWarehouseMapper.insertBsWarehouse(bsWarehouse);
    }

    /**
     * 修改仓库列表
     * 
     * @param bsWarehouse 仓库列表
     * @return 结果
     */
    @Override
    public int updateBsWarehouse(BsWarehouse bsWarehouse)
    {
        bsWarehouse.setUpdateTime(DateUtils.getNowDate());
        return bsWarehouseMapper.updateBsWarehouse(bsWarehouse);
    }

    /**
     * 批量删除仓库列表
     * 
     * @param warehouseIds 需要删除的仓库列表主键
     * @return 结果
     */
    @Override
    public int deleteBsWarehouseByWarehouseIds(Long[] warehouseIds)
    {
        return bsWarehouseMapper.deleteBsWarehouseByWarehouseIds(warehouseIds);
    }

    /**
     * 删除仓库列表信息
     * 
     * @param warehouseId 仓库列表主键
     * @return 结果
     */
    @Override
    public int deleteBsWarehouseByWarehouseId(Long warehouseId)
    {
        return bsWarehouseMapper.deleteBsWarehouseByWarehouseId(warehouseId);
    }

    @Override
    public List<BsWarehouse> selectAll() {
        return bsWarehouseMapper.selectAll();
    }
}
